<!-- 设备编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改设备':'添加设备'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
                    
      <el-form-item
        label="设备编号:"
        prop="device_no">
        <el-input
          :maxlength="20"
          v-model="form.device_no"
          placeholder="请输入设备编号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="设备名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入设备名称"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="设备类型:"
        prop="device_type">
        <el-input
          :maxlength="20"
          v-model="form.device_type"
          placeholder="请输入设备类型"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="设备类型:"
        prop="mac">
        <el-input
          :maxlength="20"
          v-model="form.mac"
          placeholder="请输入设备类型"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="标签号:"
        prop="tag_type">
        <el-input
          :maxlength="20"
          v-model="form.tag_type"
          placeholder="请输入标签号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="安装位置:"
        prop="address">
        <el-input
          :maxlength="20"
          v-model="form.address"
          placeholder="请输入安装位置"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="当前版本:"
        prop="curr_version">
        <el-input
          :maxlength="20"
          v-model="form.curr_version"
          placeholder="请输入当前版本"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>
                              
      <el-form-item
        label="流压(MPa):"
        prop="liuya">
        <el-input
          :maxlength="20"
          v-model="form.liuya"
          placeholder="请输入流压(MPa)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="标准流压(MPa):"
        prop="biao_liuya">
        <el-input
          :maxlength="20"
          v-model="form.biao_liuya"
          placeholder="请输入标准流压(MPa)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="井地温度(℃):"
        prop="jingdi_temp">
        <el-input
          :maxlength="20"
          v-model="form.jingdi_temp"
          placeholder="请输入井地温度(℃)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="套压(MPa):"
        prop="taoya">
        <el-input
          :maxlength="20"
          v-model="form.taoya"
          placeholder="请输入套压(MPa)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="液柱高度(m):"
        prop="yezhu_high">
        <el-input
          :maxlength="20"
          v-model="form.yezhu_high"
          placeholder="请输入液柱高度(m)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="瞬时起量(m2/h):"
        prop="shunshi_qiliang">
        <el-input
          :maxlength="20"
          v-model="form.shunshi_qiliang"
          placeholder="请输入瞬时起量(m2/h)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="流量计温度(℃):"
        prop="liuliang_temp">
        <el-input
          :maxlength="20"
          v-model="form.liuliang_temp"
          placeholder="请输入流量计温度(℃)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="流量计压力(Kpa):"
        prop="liuliang_yali">
        <el-input
          :maxlength="20"
          v-model="form.liuliang_yali"
          placeholder="请输入流量计压力(Kpa)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="转速(RPM):"
        prop="tran_speed">
        <el-input
          :maxlength="20"
          v-model="form.tran_speed"
          placeholder="请输入转速(RPM)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="频率(Hz):"
        prop="pinlv">
        <el-input
          :maxlength="20"
          v-model="form.pinlv"
          placeholder="请输入频率(Hz)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="电流(A):"
        prop="dianliu">
        <el-input
          :maxlength="20"
          v-model="form.dianliu"
          placeholder="请输入电流(A)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="产水(m2/h):"
        prop="chanshui">
        <el-input
          :maxlength="20"
          v-model="form.chanshui"
          placeholder="请输入产水(m2/h)"
          clearable/>
      </el-form-item>
                
      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'DeviceEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              device_no: [
          {required: true, message: '请输入设备编号', trigger: 'blur'}
        ],
                  name: [
          {required: true, message: '请输入设备名称', trigger: 'blur'}
        ],
                  device_type: [
          {required: true, message: '请输入设备类型', trigger: 'blur'}
        ],
                  mac: [
          {required: true, message: '请输入设备类型', trigger: 'blur'}
        ],
                  tag_type: [
          {required: true, message: '请输入标签号', trigger: 'blur'}
        ],
                  address: [
          {required: true, message: '请输入安装位置', trigger: 'blur'}
        ],
                  curr_version: [
          {required: true, message: '请输入当前版本', trigger: 'blur'}
        ],
                  
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
                          liuya: [
          {required: true, message: '请输入流压(MPa)', trigger: 'blur'}
        ],
                  biao_liuya: [
          {required: true, message: '请输入标准流压(MPa)', trigger: 'blur'}
        ],
                  jingdi_temp: [
          {required: true, message: '请输入井地温度(℃)', trigger: 'blur'}
        ],
                  taoya: [
          {required: true, message: '请输入套压(MPa)', trigger: 'blur'}
        ],
                  yezhu_high: [
          {required: true, message: '请输入液柱高度(m)', trigger: 'blur'}
        ],
                  shunshi_qiliang: [
          {required: true, message: '请输入瞬时起量(m2/h)', trigger: 'blur'}
        ],
                  liuliang_temp: [
          {required: true, message: '请输入流量计温度(℃)', trigger: 'blur'}
        ],
                  liuliang_yali: [
          {required: true, message: '请输入流量计压力(Kpa)', trigger: 'blur'}
        ],
                  tran_speed: [
          {required: true, message: '请输入转速(RPM)', trigger: 'blur'}
        ],
                  pinlv: [
          {required: true, message: '请输入频率(Hz)', trigger: 'blur'}
        ],
                  dianliu: [
          {required: true, message: '请输入电流(A)', trigger: 'blur'}
        ],
                  chanshui: [
          {required: true, message: '请输入产水(m2/h)', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/device/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
